package com.fishingwithme.android.ui.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fishingwithme.android.data.model.Agr
import com.fishingwithme.android.data.repository.FishingRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AgreementPolicyViewModel @Inject constructor(
    private val repository: FishingRepository
) : ViewModel() {
    
    private val _uiState = MutableStateFlow(AgreementPolicyUiState())
    val uiState: StateFlow<AgreementPolicyUiState> = _uiState.asStateFlow()
    
    fun loadContent(type: String) {
        viewModelScope.launch {
            _uiState.value = _uiState.value.copy(isLoading = true, error = null)
            repository.getAgreementOrPrivacyPolicy(type).collect { result ->
                _uiState.value = _uiState.value.copy(isLoading = false)
                result.fold(
                    onSuccess = { agr ->
                        _uiState.value = _uiState.value.copy(content = agr.content, error = null)
                    },
                    onFailure = { error ->
                        _uiState.value = _uiState.value.copy(error = error.message, content = "")
                    }
                )
            }
        }
    }
}

data class AgreementPolicyUiState(
    val isLoading: Boolean = false,
    val content: String = "",
    val error: String? = null
)